package ticket8088.config.webconfigure;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * @author ttxxi
 */
@Configuration
public class JacksonConfig {

    @Resource
    private ObjectMapper objectMapper;

    /**
     * 配置返回json时，将Long类型序列化为String类型。
     * 因为雪花算法id19位，前端number精度16位，出现精度丢失
     */
    @PostConstruct
    public void jacksonObjectMapper() {
        // 全局配置序列化返回 JSON 处理
        SimpleModule simpleModule = new SimpleModule();
        //JSON Long ==> String
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
    }

}